package frame;

import java.awt.EventQueue;


import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import bean.Manager;

import dao.DriverDao;
import dao.DriverDaoImpl;

import javax.swing.JPasswordField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UsersLoginFrame {

    private JFrame frame;
    private JTextField textField;
    private JPasswordField passwordField;

    JButton button;
    JComboBox comboBox;

    DriverDao dirverdao = new DriverDaoImpl();



    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UsersLoginFrame window = new UsersLoginFrame();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public UsersLoginFrame() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 653, 449);
        frame.setLocationRelativeTo(null); // Ĭ Ͼ     ʾ
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);
        //      м            Զ
        MYPanel bg = new MYPanel();
        //           ӵ frame  Ĭ ϴ
        frame.getContentPane().add(bg);
        //     bg Ĳ   Ϊ   Բ
        bg.setLayout(null);

        JLabel label = new JLabel("\u516C\u4EA4\u8F66\u4FE1\u606F\u7BA1\u7406\u7CFB\u7EDF");
        label.setFont(new Font("    ", Font.BOLD, 40));
        label.setBounds(125, 46, 407, 39);
        bg.add(label);

        JLabel label_1 = new JLabel("\u7528\u6237\u540D\uFF1A");
        label_1.setFont(new Font("    ", Font.BOLD, 25));
        label_1.setBounds(66, 145, 105, 52);
        bg.add(label_1);

        textField = new JTextField();
        textField.setBounds(175, 154, 127, 33);
        bg.add(textField);
        textField.setColumns(10);

        JLabel label_1_1 = new JLabel("\u5BC6\u7801\uFF1A");
        label_1_1.setFont(new Font("    ", Font.BOLD, 25));
        label_1_1.setBounds(66, 210, 105, 52);
        bg.add(label_1_1);

        passwordField = new JPasswordField();
        passwordField.setBounds(175, 219, 127, 33);
        bg.add(passwordField);

        JLabel label_1_1_1 = new JLabel("\u8EAB\u4EFD\u9009\u62E9\uFF1A");
        label_1_1_1.setFont(new Font("    ", Font.BOLD, 25));
        label_1_1_1.setBounds(66, 277, 152, 52);
        bg.add(label_1_1_1);

        comboBox = new JComboBox();
        comboBox.setFont(new Font("    ", Font.BOLD, 20));
        comboBox.setModel(new DefaultComboBoxModel(new String[] { "\u666E\u901A\u7528\u6237", "\u7BA1\u7406\u5458" }));
        comboBox.setBounds(202, 286, 127, 33);
        bg.add(comboBox);

        button = new JButton("\u767B\u5F55");
        button.setFont(new Font("    ", Font.BOLD, 25));
        button.setBounds(58, 349, 113, 27);
        bg.add(button);

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (comboBox.getSelectedItem().equals("  ͨ û ")) {
                    //     ı      ֵ
                    String username = textField.getText();
                    String upassword = passwordField.getText();
                    boolean f = dirverdao.userlogin(username, upassword);

                    if (f) {
                        JOptionPane.showMessageDialog(frame, "  ½ ɹ     ӭ  ");

                        // ҳ    ת
                        UsersEnroll ue=new UsersEnroll();
                        String []s= new String[2];
                        s[0]=username;
                        s[1]=upassword;
                        PersonalinfoFrame psf=new PersonalinfoFrame(s);
                        //  ر ԭҳ
                        frame.setVisible(false);
                    } else {
                        JOptionPane.showMessageDialog(frame, "  ½ʧ ܣ  ˺Ż        ");
                        //  ˺
                        textField.setText("");
                        passwordField.setText("");
                    }
                } else if (comboBox.getSelectedItem().equals("    Ա")) {
                    //     ı      ֵ
                    String mname = textField.getText();
                    Manager manager = dirverdao.managerlogin(mname);

                    String username = textField.getText();
                    String upassword = passwordField.getText();
                    boolean f = dirverdao.userlogin(username, upassword);
                    if (f) {
                        JOptionPane.showMessageDialog(frame, "  δ  ù   ԱȨ  ");
                        textField.setText("");
                        passwordField.setText("");
                    }//         ˫   ںžͻ
                    else if (manager.getMpwd().equals(passwordField.getText())) {
                        JOptionPane.showMessageDialog(frame, "  ½ ɹ     ӭ  ");
                        // ҳ    ת
                        ManagerEnroll me = new ManagerEnroll();
                        //  ر ԭҳ
                        frame.setVisible(false);
                    } else if(!manager.getMpwd().equals(passwordField.getText())){
                        JOptionPane.showMessageDialog(frame, "  ½ʧ ܣ  ˺Ż        ");
                        //  ˺
                        textField.setText("");
                        passwordField.setText("");
                    }
                }//

            }
        });

        JButton button_1 = new JButton("\u6CE8\u518C");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // ҳ    ת
                UsersRegisterFrame urf = new UsersRegisterFrame();
                //  ر ԭҳ
                frame.setVisible(false);
            }
        });
        button_1.setFont(new Font("    ", Font.BOLD, 25));
        button_1.setBounds(225, 349, 113, 27);
        bg.add(button_1);

        JButton button_2 = new JButton("\u6E05\u9664");
        button_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                textField.setText("");
                passwordField.setText("");
            }
        });
        button_2.setFont(new Font("    ", Font.BOLD, 25));
        button_2.setBounds(390, 349, 113, 27);
        bg.add(button_2);
    }
}

//     ͼ
class MYPanel extends JPanel {
    private Image img;

    // ʵ ֹ  췽
    public MYPanel() {
        // TODO Auto-generated constructor stub
        //  ڹ  췽   ж   Ҫ  ͼƬ  Դ   г ʼ
        ImageIcon icon = new ImageIcon(UsersLoginFrame.class.getResource("/images/  ¼ҳ 汳  ͼƬ.jpg"));
        //   icon    ת    img   ͻ
        img = icon.getImage();
    }

    // JPanel     ṩ  һ        ʵ ֡           Ĺ
    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), null);
    }

}